import OpenAI from 'openai';

// 组装文本消息的 格式
const GetTextFormat = (text: string) => {
	console.log('text', text);
	return { type: 'text', text: text };
};

// 组装图片消息的 格式
const GetImageFormat = async (base64ImageUrl: string) => {
	return { type: 'image_url', image_url: { url: base64ImageUrl } };
};

const GetOpenaiChat = async (env: Env, method: string, headers: Headers, jsonData: any) => {
	const response = await fetch(env.OpenaiUrl + '/v1/chat/completions', {
		method: method,
		headers: headers,
		body: JSON.stringify(jsonData),
	});
	return response;
};

const GetOpenaiChatByText = async (env: Env, userPromptList: any): Promise<any> => {
	const openai = new OpenAI({ apiKey: env.OpenaiApiKey });

	console.log('userPromptList', userPromptList);

	const completion = await openai.chat.completions.create({
		messages: userPromptList,
		model: env.OpenaiModel,
	});
	return completion;
};

export { GetOpenaiChat, GetOpenaiChatByText, GetTextFormat, GetImageFormat };
